package com.ruicar.afs.cloud.afscase.common.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruicar.afs.cloud.afscase.common.dto.IntelligentVoiceDto;
import com.ruicar.afs.cloud.afscase.common.feign.IntelligentVoiceFeign;
import com.ruicar.afs.cloud.afscase.common.service.IntelligentVoiceService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @description: 阿里巴巴智能语音
 * @author: mingzhi.li
 * @date: 2020/8/13 21:11
 */
@AllArgsConstructor
@Service
@Slf4j
public class IntelligentVoiceServiceImpl implements IntelligentVoiceService {
    private IntelligentVoiceFeign intelligentVoiceFeign;

    @Override
    public String getTaskId(IntelligentVoiceDto dto) {
        //请求头
        Map headers = makeHeader();
        JSONObject taskObject = new JSONObject();
        taskObject.put("appkey", dto.getAppkey());
        taskObject.put("file_link", dto.getFileLink());
        taskObject.put("token", dto.getToken());
        JSONObject json =JSONObject.parseObject(intelligentVoiceFeign.getTaskId(taskObject, headers));
        JSONObject header = (JSONObject) json.get("header");
        return header.getString("task_id");
    }

    @Override
    public JSONObject getMessageInfo(IntelligentVoiceDto dto) {
        //请求头
        Map headers = makeHeader();
        JSONObject taskObject = new JSONObject();
        taskObject.put("appkey", dto.getAppkey());
        taskObject.put("task_id", dto.getTaskId());
        taskObject.put("token", dto.getToken());
        JSONObject json =JSONObject.parseObject(intelligentVoiceFeign.getMessageInfo(taskObject, headers));
        return json;
    }


    /**
     * 获取请求头
     *
     * @return
     */
    public Map makeHeader() {
        Map headers = new HashMap<>();
        headers.put("Content-Type","application/json");
        return headers;
    }
}
